﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;

namespace Hetymine.Executioner.Plugins.Triggers.DateTime {
    /// <summary>
    /// Interaction logic for DateTimeTriggerConfiguratorWindow.xaml
    /// </summary>
    public partial class DateTimeTriggerConfiguratorWindow : Window {        
        private DateTimeTrigger _Trigger;
        private bool _IsApply = true;

        public DateTimeTriggerConfiguratorWindow() {
            InitializeComponent();
        
            this.cFullScreenQueue.Click += new RoutedEventHandler(FullScreenQueue_Click);
            this.cDaily.Click += new RoutedEventHandler(Type_Click);
            this.cWeekly.Click += new RoutedEventHandler(Type_Click);
            this.cOnce.Click += new RoutedEventHandler(Type_Click);
            this.cMonday.Click += new RoutedEventHandler(Day_Click);
            this.cTuesday.Click += new RoutedEventHandler(Day_Click);
            this.cWednesday.Click += new RoutedEventHandler(Day_Click);
            this.cThursday.Click += new RoutedEventHandler(Day_Click);
            this.cFriday.Click += new RoutedEventHandler(Day_Click);
            this.cSaturday.Click += new RoutedEventHandler(Day_Click);
            this.cSunday.Click += new RoutedEventHandler(Day_Click);
            this.cDate.SelectedDateChanged += new EventHandler<SelectionChangedEventArgs>(Date_SelectedDateChanged);
            this.cHour.TextChanged += new TextChangedEventHandler(Time_TextChanged);
            this.cMinute.TextChanged += new TextChangedEventHandler(Time_TextChanged);
            this.cSecond.TextChanged += new TextChangedEventHandler(Time_TextChanged);

            this.cOkay.Click += new RoutedEventHandler(Okay_Click);

            this.Closing += new CancelEventHandler(DateTimeTriggerConfiguratorWindow_Closing);
        }
        
        private void FullScreenQueue_Click(object sender, RoutedEventArgs e) {
            if (!this._IsApply) { return; }

            this._Trigger.FullScreenQueue = this.cFullScreenQueue.IsChecked.Value;
        }
        private void Type_Click(object sender, RoutedEventArgs e) {
            if (!this._IsApply) { return; }

            if (this.cDaily.IsChecked.Value) {
                this._Trigger.DateType = DateType.Daily;
            }
            else if (this.cWeekly.IsChecked.Value) {
                this._Trigger.DateType = DateType.Weekly;
            }
            else { // Once
                this._Trigger.DateType = DateType.Once;
                this.UpdateDate();
            }
        }
        private void Day_Click(object sender, RoutedEventArgs e) {
            if (!this._IsApply) { return; }

            Days days = Days.None;
            
            if (this.cMonday.IsChecked.Value) {
                days |= Days.Monday;
            }

            if (this.cTuesday.IsChecked.Value) {
                days |= Days.Tuesday;
            }

            if (this.cWednesday.IsChecked.Value) {
                days |= Days.Wednesday;
            }

            if (this.cThursday.IsChecked.Value) {
                days |= Days.Thursday;
            }

            if (this.cFriday.IsChecked.Value) {
                days |= Days.Friday;
            }

            if (this.cSaturday.IsChecked.Value) {
                days |= Days.Saturday;
            }

            if (this.cSunday.IsChecked.Value) {
                days |= Days.Sunday;
            }

            this._Trigger.Days = days;
        }
        private void Date_SelectedDateChanged(object sender, SelectionChangedEventArgs e) {
            if (!this._IsApply) { return; }

            this.UpdateDate();
        }
        private void Time_TextChanged(object sender, TextChangedEventArgs e) {
            if (!this._IsApply) { return; }
            
            System.DateTime date = this._Trigger.DateTime.Date;
                        
            // Hours
            int time = (int)this.cHour.Value;
            date = date.AddHours(time);            
            
            // Minutes
            time = (int)this.cMinute.Value;
            date = date.AddMinutes(time);

            // Seconds
            time = (int)this.cSecond.Value;
            date = date.AddSeconds(time);

            this._Trigger.DateTime = date;
        }

        private void Okay_Click(object sender, RoutedEventArgs e) {
            this.Hide();
        }

        private void DateTimeTriggerConfiguratorWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
            e.Cancel = true;
            this.Hide();
        }

        private void UpdateDate() {
            System.DateTime date;

            if (this.cDate.SelectedDate == null) {
                date = System.DateTime.Today;
            }
            else {
                date = this.cDate.SelectedDate.Value;
            }

            date = date.AddHours(this._Trigger.DateTime.Hour);
            date = date.AddMinutes(this._Trigger.DateTime.Minute);
            date = date.AddSeconds(this._Trigger.DateTime.Second);

            this._Trigger.DateTime = date;
        }

        public void InitializeTrigger(DateTimeTrigger trigger) {
            this._Trigger = trigger;

            this._IsApply = false;

            this.cFullScreenQueue.IsChecked = this._Trigger.FullScreenQueue;

            this.cDaily.IsChecked = false;
            this.cWeekly.IsChecked = false;
            this.cOnce.IsChecked = false;

            this.cMonday.IsChecked = false;
            this.cTuesday.IsChecked = false;
            this.cWednesday.IsChecked = false;
            this.cThursday.IsChecked = false;
            this.cFriday.IsChecked = false;
            this.cSaturday.IsChecked = false;
            this.cSunday.IsChecked = false;

            this.cDate.SelectedDate = System.DateTime.Today;

            switch (this._Trigger.DateType) {
                case DateType.Daily:
                    this.cDaily.IsChecked = true;
                    break;
                case DateType.Weekly:
                    this.cWeekly.IsChecked = true;
                    if ((this._Trigger.Days | Days.Monday) == this._Trigger.Days) {
                        this.cMonday.IsChecked = true;
                    }
                    if ((this._Trigger.Days | Days.Tuesday) == this._Trigger.Days) {
                        this.cTuesday.IsChecked = true;
                    }
                    if ((this._Trigger.Days | Days.Wednesday) == this._Trigger.Days) {
                        this.cWednesday.IsChecked = true;
                    }
                    if ((this._Trigger.Days | Days.Thursday) == this._Trigger.Days) {
                        this.cThursday.IsChecked = true;
                    }
                    if ((this._Trigger.Days | Days.Friday) == this._Trigger.Days) {
                        this.cFriday.IsChecked = true;
                    }
                    if ((this._Trigger.Days | Days.Saturday) == this._Trigger.Days) {
                        this.cSaturday.IsChecked = true;
                    }
                    if ((this._Trigger.Days | Days.Sunday) == this._Trigger.Days) {
                        this.cSunday.IsChecked = true;
                    }
                    break;
                case DateType.Once:
                    this.cOnce.IsChecked = true;

                    break;
            }

            this.cHour.Text = this._Trigger.DateTime.Hour.ToString();
            this.cMinute.Text = this._Trigger.DateTime.Minute.ToString();
            this.cSecond.Text = this._Trigger.DateTime.Second.ToString();

            this._IsApply = true;
        }
    }
}
